package ca.liu.scaffoldingmvctest.web.rest;

import ca.liu.scaffoldingmvctest.dao.StLogDAO;
import ca.liu.scaffoldingmvctest.dao.StUserDAO;

import ca.liu.scaffoldingmvctest.domain.StLog;
import ca.liu.scaffoldingmvctest.domain.StUser;

import ca.liu.scaffoldingmvctest.service.StUserService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Spring Rest controller that handles CRUD requests for StUser entities
 * 
 */

@Controller("StUserRestController")
public class StUserRestController {

	/**
	 * DAO injected by Spring that manages StLog entities
	 * 
	 */
	@Autowired
	private StLogDAO stLogDAO;

	/**
	 * DAO injected by Spring that manages StUser entities
	 * 
	 */
	@Autowired
	private StUserDAO stUserDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for StUser entities
	 * 
	 */
	@Autowired
	private StUserService stUserService;

	/**
	 * Show all StLog entities by StUser
	 * 
	 */
	@RequestMapping(value = "/StUser/{stuser_id}/stLogs", method = RequestMethod.GET)
	public List<StLog> getStUserStLogs(@PathVariable Integer stuser_id) {
		return new java.util.ArrayList<StLog>(stUserDAO.findStUserByPrimaryKey(stuser_id).getStLogs());
	}

	/**
	 * Save an existing StUser entity
	 * 
	 */
	@RequestMapping(value = "/StUser", method = RequestMethod.PUT)
	public StUser saveStUser(@ModelAttribute StUser stuser) {
		stUserService.saveStUser(stuser);
		return stUserDAO.findStUserByPrimaryKey(stuser.getId());
	}

	/**
	 * View an existing StLog entity
	 * 
	 */
	@RequestMapping(value = "/StUser/{stuser_id}/stLogs/{stlog_id}", method = RequestMethod.GET)
	public StLog loadStUserStLogs(@PathVariable Integer stuser_id, @PathVariable Integer related_stlogs_id) {
		StLog stlog = stLogDAO.findStLogByPrimaryKey(related_stlogs_id, -1, -1);

		return stlog;
	}

	/**
	 * Delete an existing StLog entity
	 * 
	 */
	@RequestMapping(value = "/StUser/{stuser_id}/stLogs/{stlog_id}", method = RequestMethod.DELETE)
	public void deleteStUserStLogs(@PathVariable Integer stuser_id, @PathVariable Integer related_stlogs_id) {
		stUserService.deleteStUserStLogs(stuser_id, related_stlogs_id);
	}

	/**
	 * Show all StUser entities
	 * 
	 */
	@RequestMapping(value = "/StUser", method = RequestMethod.GET)
	public List<StUser> listStUsers() {
		return new java.util.ArrayList<StUser>(stUserService.loadStUsers());
	}

	/**
	 * Delete an existing StUser entity
	 * 
	 */
	@RequestMapping(value = "/StUser/{stuser_id}", method = RequestMethod.DELETE)
	public void deleteStUser(@PathVariable Integer stuser_id) {
		StUser stuser = stUserDAO.findStUserByPrimaryKey(stuser_id);
		stUserService.deleteStUser(stuser);
	}

	/**
	 * Create a new StUser entity
	 * 
	 */
	@RequestMapping(value = "/StUser", method = RequestMethod.POST)
	public StUser newStUser(@ModelAttribute StUser stuser) {
		stUserService.saveStUser(stuser);
		return stUserDAO.findStUserByPrimaryKey(stuser.getId());
	}

	/**
	 * Select an existing StUser entity
	 * 
	 */
	@RequestMapping(value = "/StUser/{stuser_id}", method = RequestMethod.GET)
	public StUser loadStUser(@PathVariable Integer stuser_id) {
		return stUserDAO.findStUserByPrimaryKey(stuser_id);
	}

	/**
	 * Create a new StLog entity
	 * 
	 */
	@RequestMapping(value = "/StUser/{stuser_id}/stLogs", method = RequestMethod.POST)
	public StLog newStUserStLogs(@PathVariable Integer stuser_id, @ModelAttribute StLog stlog) {
		stUserService.saveStUserStLogs(stuser_id, stlog);
		return stLogDAO.findStLogByPrimaryKey(stlog.getId());
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Save an existing StLog entity
	 * 
	 */
	@RequestMapping(value = "/StUser/{stuser_id}/stLogs", method = RequestMethod.PUT)
	public StLog saveStUserStLogs(@PathVariable Integer stuser_id, @ModelAttribute StLog stlogs) {
		stUserService.saveStUserStLogs(stuser_id, stlogs);
		return stLogDAO.findStLogByPrimaryKey(stlogs.getId());
	}
}